﻿//Customer.cpp
#include"Customer.hh"
#include<fstream>
#include<iomanip>
#include<sstream>
#include<chrono>
#ifdef _WIN32
#include<Windows.h>
#endif
namespace Banking {

    //客户类的构造函数实现
    Customer::Customer(const std::string& name)
        : name(name)
    {
    }

    //生成客户交易报告的实现
    void Customer::generateTransactionReport() const {
        SetConsoleOutputCP(CP_UTF8);//Windows系统下设置控制台输出为UTF-8编码
        auto now = std::chrono::system_clock::now();  //获取当前时间
        auto time = std::chrono::system_clock::to_time_t(now);
        std::tm localTime;
        localtime_s(&localTime, &time);
        std::stringstream ss;
        ss << std::put_time(&localTime, "%Y-%m-%d");

        std::string filename = "transactions_" + name + "_" + ss.str() + ".txt";  //生成报告文件名
        std::ofstream file(filename);  //打开文件

        if (!file) {
            return;
        }

        //写入报告头部信息
        file << "Transaction Report for Customer: " << name << "\n\n";

        //写入储蓄账户信息和交易记录
        if (savingsAccount) {
            file << "Savings Account: " << savingsAccount->getAccountId() << "\n"
                << "Current Balance: ¥" << std::fixed << std::setprecision(2)
                << savingsAccount->getBalance() << "\n\n";

            for (const auto& transaction : savingsAccount->getTransactions()) {
                file << transaction.toString() << "\n";
            }
        }

        //写入支票账户信息和交易记录
        if (checkingAccount) {
            SetConsoleOutputCP(CP_UTF8);//Windows系统下设置控制台输出为UTF-8编码
            file << "Checking Account: " << checkingAccount->getAccountId() << "\n"
                << "Current Balance: ¥" << std::fixed << std::setprecision(2)
                << checkingAccount->getBalance() << "\n\n";

            for (const auto& transaction : checkingAccount->getTransactions()) {
                file << transaction.toString() << "\n";
            }
        }
    }

} //namespace Banking